<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
      <div class="paper-cut-pattern pattern-1"></div>
      <div class="paper-cut-pattern pattern-2"></div>
      <div class="paper-cut-pattern pattern-3"></div>
      <div class="paper-cut-pattern pattern-4"></div>
      <div class="paper-cut-pattern pattern-5"></div>
    </div>
    
    <!-- 登录表单 -->
    <div class="login-wrapper">
      <div class="login-card">
        <div class="login-header">
          <div class="logo-section">
            <div class="logo-icon">✂️</div>
            <h1 class="system-title">剪纸文化展示平台</h1>
            <p class="system-subtitle">后台管理系统</p>
          </div>
        </div>
        
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
          <el-form-item prop="username">
            <div class="input-wrapper">
              <i class="input-icon">👤</i>
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名" 
                size="large"
                class="custom-input"
                @keyup.enter="handleLogin"
              />
            </div>
          </el-form-item>
          
          <el-form-item prop="password">
            <div class="input-wrapper">
              <i class="input-icon">🔒</i>
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="请输入密码" 
                show-password
                size="large"
                class="custom-input"
                @keyup.enter="handleLogin"
              />
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              @click="handleLogin" 
              :loading="loading" 
              size="large"
              class="login-button"
              :disabled="loading"
            >
              {{ loading ? '登录中...' : '登 录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <p class="copyright">© 2025 中国剪纸文化展示平台</p>
          <p class="description">传承中华文化 · 弘扬民族艺术</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { webLogin } from '@/api/user'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)
    const loading = ref(false)
    
    const loginForm = reactive({
      username: 'admin',
      password: 'admin'
    })
    
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      await loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            const response = await webLogin(loginForm)
            // 保存token到localStorage
            localStorage.setItem('token', response.data.token)
            // 保存用户信息
            localStorage.setItem('userInfo', JSON.stringify(response.data.userInfo))
            
            ElMessage.success('登录成功')
            
            // 获取重定向地址，如果没有则跳转到首页
            const redirect = router.currentRoute.value.query.redirect || '/'
            router.push(redirect)
          } catch (error) {
            console.error('登录失败:', error)
            ElMessage.error('登录失败: ' + (error.message || '用户名或密码错误'))
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    return {
      loginForm,
      loginRules,
      loginFormRef,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #8B0000 0%, #DC143C 50%, #B22222 100%);
  position: relative;
  overflow: hidden;
  font-family: 'Microsoft YaHei', Arial, sans-serif;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  right: -100px;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -80px;
  left: -80px;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 30%;
  left: 10%;
}

/* 剪纸图案 */
.paper-cut-pattern {
  position: absolute;
  background: transparent;
  border: 2px solid rgba(255, 255, 255, 0.3);
  animation: float 6s ease-in-out infinite;
}

/* 不同形状的剪纸图案 */
.pattern-1 {
  width: 80px;
  height: 80px;
  top: 15%;
  right: 20%;
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  animation: float 6s ease-in-out infinite;
}

.pattern-2 {
  width: 60px;
  height: 60px;
  bottom: 25%;
  left: 20%;
  clip-path: polygon(50% 0%, 80% 10%, 100% 35%, 90% 70%, 50% 100%, 10% 70%, 0% 35%, 20% 10%);
  animation: float 8s ease-in-out infinite 1s;
}

.pattern-3 {
  width: 70px;
  height: 70px;
  top: 40%;
  left: 50%;
  clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
  animation: float 7s ease-in-out infinite 0.5s;
}

.pattern-4 {
  width: 50px;
  height: 50px;
  top: 20%;
  left: 15%;
  clip-path: circle(50%);
  animation: float 9s ease-in-out infinite 1.5s;
}

.pattern-5 {
  width: 90px;
  height: 90px;
  bottom: 15%;
  right: 15%;
  clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
  animation: float 10s ease-in-out infinite 2s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 登录容器 */
.login-wrapper {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 40px 30px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  overflow: hidden;
}

/* 添加剪纸风格的边框装饰 */
.login-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 2px solid transparent;
  border-image: repeating-linear-gradient(
    45deg,
    #8B0000,
    #8B0000 10px,
    transparent 10px,
    transparent 20px
  ) 2;
  pointer-events: none;
  border-radius: 20px;
}

/* 头部Logo区域 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-section {
  margin-bottom: 20px;
}

.logo-icon {
  font-size: 48px;
  margin-bottom: 15px;
  animation: pulse 2s infinite;
  display: inline-block;
  filter: drop-shadow(0 0 5px rgba(139, 0, 0, 0.3));
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

.system-title {
  font-size: 24px;
  font-weight: 600;
  color: #8B0000;
  margin: 10px 0 5px;
  letter-spacing: 1px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.system-subtitle {
  font-size: 16px;
  color: #666;
  margin: 0;
}

/* 表单样式 */
.login-form {
  margin-bottom: 30px;
}

.input-wrapper {
  position: relative;
  margin-bottom: 20px;
}

.input-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 18px;
  color: #999;
  z-index: 2;
}

.custom-input {
  width: 100%;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 25px;
  padding: 0 20px 0 45px;
  height: 50px;
  border: 2px solid #e0e0e0;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #8B0000;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #8B0000;
  box-shadow: 0 0 0 2px rgba(139, 0, 0, 0.1);
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #8B0000 0%, #DC143C 100%);
  border: none;
  border-radius: 25px;
  color: white;
  letter-spacing: 2px;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(139, 0, 0, 0.3);
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: 0.5s;
}

.login-button:hover:not(:disabled)::before {
  left: 100%;
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(139, 0, 0, 0.4);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

/* 底部信息 */
.login-footer {
  text-align: center;
  margin-top: 20px;
}

.copyright {
  color: #8B0000;
  font-size: 14px;
  margin: 0 0 5px;
  font-weight: 500;
}

.description {
  color: #666;
  font-size: 12px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-wrapper {
    padding: 10px;
  }
  
  .login-card {
    padding: 30px 20px;
  }
  
  .system-title {
    font-size: 20px;
  }
  
  .system-subtitle {
    font-size: 14px;
  }
  
  .paper-cut-pattern {
    width: 50px;
    height: 50px;
  }
  
  .circle-1 {
    width: 200px;
    height: 200px;
  }
  
  .circle-2 {
    width: 150px;
    height: 150px;
  }
  
  .pattern-1 {
    width: 60px;
    height: 60px;
    right: 10%;
  }
  
  .pattern-2 {
    width: 45px;
    height: 45px;
    left: 10%;
  }
  
  .pattern-3 {
    width: 55px;
    height: 55px;
    left: 40%;
  }
}
</style>